package com.qf.blog.web.controller;

import com.qf.blog.core.exception.JsonException;
import com.qf.blog.web.entity.CommentEntity;
import com.qf.blog.web.entity.UserEntity;
import com.qf.blog.web.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/commentController")
public class CommentController {

    @Autowired
    private ICommentService commentService;

    /**
     *
     * @param id 帖子id
     * @param commentEntity
     * @param session
     * @return
     */
    @PostMapping("/add/{id}")
    public String add(@PathVariable Integer id, CommentEntity commentEntity, HttpSession session) {

        UserEntity loginUser = (UserEntity) session.getAttribute("loginUser");
        if (loginUser == null) {
            throw new JsonException(100, "请先登录");
        }

        // 1.设置发帖人
        commentEntity.setUserId(loginUser.getId());

        // 2.入库
        commentService.save(commentEntity);

        // 5.重定向当前帖子下面
        return "redirect:/invitataionController/info/" + id;
    }
}
